package com.zrh.jsd.service.studydemo.queue_test;

import com.zrh.jsd.service.studydemo.threadpool.A;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;

public class LinkedBlockingQueueTest {
    public static void main(String[] args) {
        ArrayBlockingQueue queue = new ArrayBlockingQueue(10);

        new Thread(()->{
            int i = 0;
            try {
                while (true){
                    System.out.println("放入i=="+i);
                    queue.put(i++);
                    Thread.currentThread().sleep(1000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(()->{
            try {
                while (true){
                    List<Object> list = new ArrayList<>();
                    Thread.currentThread().sleep(2000);
                     queue.drainTo(list);
                    list.stream().forEach(System.out::println);
                    for (Object o : list) {
                        System.out.println("取出==="+o.toString());
                    }
                    Iterator iterator = queue.iterator();
                    while (iterator.hasNext()){
                        System.out.println(iterator.next());
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
